//
//  MBIMSelectedVC.m
//  发布求租
//
//  Created by MB__Lee on 2018/6/9.
//  Copyright © 2018年 MB__Lee. All rights reserved.
//

#import "MBIMSelectedVC.h"

@interface MBIMSelectedVC ()<UIActionSheetDelegate,UIImagePickerControllerDelegate,UINavigationControllerDelegate>
@property (weak, nonatomic) IBOutlet UIImageView *imgView;

@property (weak, nonatomic) IBOutlet UILabel *titlelabel;

@property(nonatomic,strong)UIImagePickerController *picker;

@property(nonatomic,strong)UIImage *img; // 要上传的 图片

@end

@implementation MBIMSelectedVC
-(UIImagePickerController *)picker{
    if (!_picker) {
        UIImagePickerController *picker = [[UIImagePickerController alloc]init];
        
        _picker = picker;
//        picker.sourceType = UIImagePickerControllerSourceTypeCamera;
        picker.delegate = self;
        picker.allowsEditing = YES;
    }
    return  _picker;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
        self.titlelabel.text = self.titleName;
    
    
    //1.添加手势
    [self addGesture];
    
}
-(void)addGesture{
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap:)];
    self.imgView.userInteractionEnabled = YES;
    [self.imgView addGestureRecognizer:tap];
}
-(void)tap:(UITapGestureRecognizer *)tap{
    
    UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"请选择上传方式" delegate:self cancelButtonTitle:@"cancel" destructiveButtonTitle:@"拍照" otherButtonTitles:@"相册",nil];
    [actionSheet showInView:self.view];
    
}

-(void)setTitleName:(NSString *)titleName{
    _titleName = titleName;
    
}

- (IBAction)comitIMG:(id)sender {
    
    if ([self.delegate respondsToSelector:@selector(MBIMSelectedVC:sendImage:)]) {
        [self.delegate MBIMSelectedVC:self sendImage:self.img];
        [self.navigationController popViewControllerAnimated:YES];
    }
    
    
}


#pragma mark ---UIActionSheetDelegate
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{
    
    if (buttonIndex == 0) { // 拍照
        self.picker.sourceType = UIImagePickerControllerSourceTypeCamera;
        
    }else if(buttonIndex == 1){ // 相册
        self.picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    }
    
//    [self.navigationController pushViewController:self.picker animated:YES];
    [self presentViewController:self.picker animated:YES completion:nil];
}
#pragma mark --UIImagePickerControllerDelegate
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{
    
    UIImage *editImage = info[@"UIImagePickerControllerEditedImage"];
    self.img = editImage;
    self.imgView.image = editImage;
    self.imgView.contentMode =  UIViewContentModeScaleAspectFill;
    self.imgView.clipsToBounds = YES;
    
    
    [picker dismissViewControllerAnimated:YES completion:nil];
}
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary<NSString *,id> *)editingInfo{
    
    
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.translucent = NO;
}
-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    self.navigationController.navigationBar.translucent = YES;
}



/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
